import {generateName} from '@/common/util/upload.js'
import {UserIdFiles,RecorderConfig} from '@/common/chat_config.js'
export default{
	data() {
		return{
			RECORDER:null,//录音器对像
			isVoice:false,//是否是使录音功能
			voiceTis:'按住 说话',//录音框提示文本
			recordTis:"手指上滑 取消发送",//取消框提示文本
			recording:false,//正在录音
			willStop:false,//停止录音
			initPoint:{identifier:0,Y:0},
			recordTimer:null,//录音记时器ID setInterval 返回的ID
			recordLength:0,//录音时长
		}
	},
	onLoad:function(e){
		// #ifndef H5 || MP-ALIPAY
		this.RECORDER=uni.getRecorderManager()
		//录音开始事件
		this.RECORDER.onStart((e)=>{
			this.recordBegin(e);
		})
		//录音结束事件
		this.RECORDER.onStop((e)=>{
			this.recordEnd(e);
		})
		// #endif
	},
	destroyed:function(){
		/* 
		 @touchstart="voiceBegin"
		 @touchmove.stop.prevent="voiceIng" 
		 @touchend="voiceEnd" 
		 @touchcancel="voiceCancel"*/
	},
	methods:{
		//触摸开始
		voiceBegin(e){
			//多指触摸，直接无视
			if(e.touches.length>1){
				return ;
			}
			
			this.initPoint.Y = e.touches[0].clientY;
			this.initPoint.identifier = e.touches[0].identifier;
			this.RECORDER.start({format:RecorderConfig.ext});//录音开始,
		},
		//触摸移动时
		voiceIng(e){
			if(!this.recording){
				return;
			}
			let touche = e.touches[0];
			if(this.initPoint.Y - touche.clientY>=uni.upx2px(100)){
				this.willStop = true;
				this.recordTis = '松开手指 取消发送'
			}else{
				this.willStop = false;
				this.recordTis = '手指上滑 取消发送'
			}
		},
		//结束触摸
		voiceEnd(e){
			if(!this.recording){
				return;
			}
			this.recording = false;
			this.voiceTis='按住 说话';
			this.recordTis = '手指上滑 取消发送'
			this.RECORDER.stop();//录音结束
		},
		//触摸取消
		voiceCancel(e){
			this.recording = false;
			this.voiceTis='按住 说话';
			this.recordTis = '手指上滑 取消发送'
			this.willStop = true;//不发送录音
			this.RECORDER.stop();//录音结束
		},
		//开始录音
		recordBegin(e){
			this.recording = true;
			this.voiceTis='松开 结束';
			this.recordLength = 1;
			//录音计时器
			this.recordTimer = setInterval(()=>{
				if(this.recordLength>60){
					this.recording = false;
					this.voiceTis='最大录音60秒';
					this.RECORDER.stop();//录音结束
				}
				this.recordLength++;
			},1000)
		},
		//录间结束
		recordEnd(e){
			clearInterval(this.recordTimer);
			if(!this.willStop){
				this.sendVoice(e.tempFilePath,this.recordLength);
			}else{
				console.log('取消发送录音');
			}
			this.willStop = false;
		},
		sendVoice(filePath,recordLength){
			this.voiceTis='按住 说话';
			this.recordTis = '手指上滑 取消发送'
			const now= (new Date).getTime()+600000,cloudPath=generateName()+'.'+RecorderConfig.ext,self=this;
			// 判断token是否过期
			if(this.userInfo && this.userInfo.tokenExpired &&  this.userInfo.tokenExpired >now){
				 uniCloud.uploadFile({filePath,cloudPath,fileType:'audio'})
				 .then(res=>{
					const {fileID,success}=res
					if(success){
						const db= uniCloud.database()
						const data={name:cloudPath,cloudPath,fileID,recordLength,type:RecorderConfig.type}
						db.collection(UserIdFiles).add(data)
						.then(res=>{
							self.send({url:fileID,duration:recordLength},'voice')
						})
						.catch(e=>console.log(e))
					}
					
				})
				
				
				
			}
		}
	}
	
}